package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.GoodsDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    ProductConverter productConverter;
    /**
     *@
     *@author yuyujian
     *@Date 2022/10/26
     *@Time 17:39
     */

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Override
    public ProductDetailDto getProductDetail(Long id) {
        //根据商品id获取商品参数
        Item item = itemMapper.selectByPrimaryKey(id);
        //根据商品id获取商品细节
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        //转化
        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);
        return productDetailDto;
    }

    /**@Description:分页查询商品列表
     * @author chengyi
     * @date 2022/10/26 19:51
     */
    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        //创建实例
        Example example = new Example(Item.class);

        //如果sort = 1，则升序:ASC，sort = -1，降序:DESC
        //null 则按照商品的创建时间created字段进行排序，且为降序
        String sort = pageInfo.getSort();


        if ("1".equals(sort)) {
            example.setOrderByClause("price ASC");

        } else if ("-1".equals(sort)) {
            example.setOrderByClause("price DESC");

        } else if(sort == null){
            example.setOrderByClause("created DESC");
        }

//        //获取数据库中所有的商品列表
//        List<Item> items = itemMapper.selectAll();

        //按照最小值的最大值进行筛选
        Example.Criteria criteria = example.createCriteria();
        Integer priceGt = pageInfo.getPriceGt();
        Integer priceLte = pageInfo.getPriceLte();

//        //与每一个商品的价格做对比
//        if (priceGt != null && !"".equals(priceGt) || priceLte != null && !"".equals(priceLte)){
//            for (Item item : items) {
//                //获取商品的价格
//                BigDecimal price1 = item.getPrice();
//                String price = price1.toString();
//
//                //对存在的情况判断
//                if (priceGt != null && !"".equals(priceGt) && (priceLte == null || "".equals(priceLte))) {
//                    //仅有最小值
//                    criteria.andGreaterThanOrEqualTo(price, priceGt);
//
//                } else if ((priceGt == null || "".equals(priceGt)) && priceLte != null && !"".equals(priceLte)) {
//                    // 仅有最大值
//                    criteria.andLessThanOrEqualTo(price, priceLte);
//
//                } else if (priceGt != null && !"".equals(priceGt) && priceLte != null && !"".equals(priceLte) && priceGt < priceLte) {
//                    //两者都存在并且最小值小于最大值
//                    criteria.andBetween(price, priceGt, priceLte);
//                }
//            }
//        }

        //与每一个商品的价格做对比
        if (priceGt != null && !"".equals(priceGt) || priceLte != null && !"".equals(priceLte)){
            //对存在的情况判断
            if (priceGt != null && !"".equals(priceGt) && (priceLte == null || "".equals(priceLte))) {
                //仅有最小值
                criteria.andGreaterThanOrEqualTo("price", priceGt);

            } else if ((priceGt == null || "".equals(priceGt)) && priceLte != null && !"".equals(priceLte)) {
                // 仅有最大值
                criteria.andLessThanOrEqualTo("price", priceLte);

            } else if (priceGt != null && !"".equals(priceGt) && priceLte != null && !"".equals(priceLte) && priceGt < priceLte) {
                //两者都存在并且最小值小于最大值
                criteria.andBetween("price", priceGt, priceLte);
            }
        }


        //访问数据库获取数据
        List<Item> list = itemMapper.selectByExample(example);
        PageInfo<Item> pageInfos = new PageInfo<>(list);
        //获取总条数
        long total = pageInfos.getTotal();

        //使用pageHelper开启分页
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getSize());

        //对象类型转换
        List<ProductDto> productDtos = productConverter.items2Dto(list);

        AllProductResponse productResponse = new AllProductResponse();
        productResponse.setData(productDtos);
        productResponse.setTotal(total);

        return productResponse;
    }

    /**@Description:查询推荐商品
     * @author chengyi
     * @date 2022/10/26 20:04
     */
    @Override
    public Set<PanelDto> getRecommendGoods() {

        //获取数据库的商品列表
        Panel panel = panelMapper.selectByPrimaryKey(6);
        //对象类型转化
        PanelDto panelDto = contentConverter.panel2Dto(panel);

        Integer id = panelDto.getId();


        List<PanelContentItem> panelContentItemList = panelContentMapper.selectPanelContentAndProductWithPanelId(id);

        //对象类型转化
        List<PanelContentItemDto> panelContentItemDto = contentConverter.panelContentItem2Dto(panelContentItemList);

        panelDto.setPanelContentItems(panelContentItemDto);

        HashSet<PanelDto> panelDtos = new HashSet<>();
        panelDtos.add(panelDto);

        return panelDtos;
    }

    /**
     * 获取全部商品的业务逻辑
     * @return java.util.List<com.cskaoyan.mall.dto.ItemDto>
     * @author gatsby
     * @since 2022/11/02 17:09
     */
    @Override
    public List<ItemDto> getAllItems() {
        List<Item> items = itemMapper.selectAll();
        List<ItemDto> itemDtos = productConverter.items2ItemDtos(items);


        return itemDtos;
    }
}
